<?php
class Ext_Controller_Action extends Zend_Controller_Action
{
	const NOT_FOUND_APP_ID = 'Es necesario agregar en la configuracion el Application Id correspondiente';
	
	/**
	 * Esta variable contiene el numero de pagina, sirve para el paginator
	 * @var unknown_type
	 */
	protected $_page;
	
    protected $_applicationId;
    /**
     * This property stores the Zend_Session_Namespace object for the application
     *
     * @var Zend_Session_Namespace
     */
    protected $_session;

    /**
     * This property stores an instance for the applications config
     *
     * @var Ext_Generic_Config
     */
    protected $_config;

    /**
     * True if in debug mode, readed from application.ini
     *
     * @var boolean
     */
    protected $_debug = false;

    /**
     * This property stores an instance for the applications default db connection
     *
     * @var Zend_Db_Adapter_Abstract
     */
    protected $_db;

    /**
     * Bootstrap to get resources
     *
     * @var Zend_Application
     */
    protected $_bootstrap;

    public function init()
    {
        $this->_bootstrap = $this->getFrontController()->getParam('bootstrap');
        if( isset( $this->_bootstrap->config)){
            $this->_config = $this->_bootstrap->config;
        }
        if( isset( $this->_bootstrap->db)){
            $this->_db = $this->_bootstrap->db;
        }
        if( isset( $this->_bootstrap->sessionStart)){
            $this->_session = $this->_bootstrap->sessionStart;
        }

        if( empty( $this->_config->main->app->id )) {
        	throw new Ext_Exception( self::NOT_FOUND_APP_ID );
        } else {
        	$this->_applicationId = $this->_config->main->app->id; 
       	}
        $env = $this->_bootstrap->getEnvironment();
        $this->_debug = ( strncasecmp( $env, 'PROD', 4 ) != 0 );
        $this->view->staticServer = $this->getFrontController()->getBaseUrl();
		
        /* seteamos la pagina actual */
        $this->_page = ( $this->_hasParam( 'page' ) ) ? $this->_getParam( 'page' ) : 1;
    }

    public function getResource( $resourceName ) {
        if ( empty( $this->_bootstrap ) ) {
            $this->_bootstrap = $this->getFrontController()->getParam('bootstrap');
        }
        return $this->_bootstrap->getResource( $resourceName );
    }
    /**
     * Devuelve true si la aplicacion esta siendo debugeada.
     *
     * @return bool true si la aplicacion esta siendo debugueada
     */
    public function isInDebugMode()
    {
        return $this->_debug;
    }

    /**
     * Agrega un error al layout
     *
     * @param string $msg
     */
    protected function addError( $msg )
    {
        $this->_helper->getHelper('ExtFlashMessenger')->addError( $msg );
    }

    /**
     * Agrega un suceso al layout
     *
     * @param string $msg
     */
    protected function addSuccess( $msg )
    {
        $this->_helper->getHelper('ExtFlashMessenger')->addSuccess( $msg );
    }

    /**
     * Agrega una advertencia al layout
     *
     * @param string $msg
     */
    protected function addWarning( $msg )
    {
        $this->_helper->getHelper('ExtFlashMessenger')->addWarning( $msg );
    }

    /**
     * Agrega una  noticia al layout
     *
     * @param string $msg
     */
    protected function addNotice( $msg )
    {
        $this->_helper->getHelper('ExtFlashMessenger')->addNotice( $msg );
    }

}
